package com.canbot.u05.activity.customvoice;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.bean.QuestionBean;
import com.canbot.u05.bean.CustomDialogue;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * liu.heng
 * 选择自定义对话定制方式
 */
public class ChooseAddTypeActivity extends BaseActivityNoTitle {

        private static final String TAG = "ChooseAddTypeActivity";

        private List<QuestionBean> mListDatas;

        private Dialog dialogLoading;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_choose_add_type);
                mListDatas = new ArrayList<>();
        }

        @Override
        protected void onResume() {
                super.onResume();
                mListDatas.clear();
                final List<CustomDialogue> questionList = new ArrayList<>();
                for (int i = 0; i < ((MainApplication) getApplication()).mCustomDialogues.size(); i++) {
                        questionList.add(((MainApplication) getApplication()).mCustomDialogues.get(i));
                }
                dialogLoading = LoadingDialogUtils.createLoadingDialog(ChooseAddTypeActivity.this, getString(R.string.loading));
                dialogLoading.show();
                final String url = "http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "dialogcustom";
                final HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("type", "5");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @SuppressLint("LongLogTag")
                        @Override
                        public void onSuccess(String result) {
                                dialogLoading.dismiss();
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);

                                if (strMsgBean.getRc() != 0) {
                                        ToastTools.newInstance(getApplicationContext()).showText(ChooseAddTypeActivity.this.getString(R.string.network_error));
                                        return;
                                }

                                final List<QuestionBean> questionBeanList = JSON.parseArray(strMsgBean.getResult().toString(), QuestionBean.class);
                                for (int i = 0; i < questionBeanList.size(); i++) {
                                        Log.e(TAG, "---------->" + questionBeanList.get(i) + "");
                                        if (questionBeanList.get(i).getType() == 1) {
                                                mListDatas.add(questionBeanList.get(i));
                                        }
                                }
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                if (mListDatas.size() > 0) {
                                                        SPUtils.saveBoolean(ChooseAddTypeActivity.this, "isHaveData", true);
                                                        startActivity(new Intent(ChooseAddTypeActivity.this, CustomDialogListActivity.class));
                                                        finish();
                                                }
                                                else {
                                                        SPUtils.saveBoolean(ChooseAddTypeActivity.this, "isHaveData", false);
                                                }
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(getApplicationContext()).showText(ChooseAddTypeActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });
        }

        public void single(View view) {
                startActivity(new Intent(this, CustomQuestionActivity.class));
        }

        public void bulk(View view) {
                startActivity(new Intent(this, CustomDialogActivity.class).putExtra("type", 0));
        }

        public void back(View view) {
                finish();
        }
}
